package com.flyee.viewer
{
	import com.flyee.service.CharactorService;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.GenerateIDTool;
	import com.flyee.utils.PlatformDepot;
	import com.flyee.viewer.pack.helper.ItemDataModel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import mx.collections.ArrayCollection;
	public class SkillPanelViewer extends Sprite implements Destroy
	{
		public var vid:String = GenerateIDTool.generateID();
		
		public var drugItems:ArrayCollection = new ArrayCollection();
		/**
		 * 技能框的宽和高 
		 */		
		private const itemWH:int = 36;
		public var skillBtn1:SkillButtonViewer;
		public var skillBtn2:SkillButtonViewer;
		public var skillBtn3:SkillButtonViewer;
		public var skillBtn4:SkillButtonViewer;
		public var skillBtn5:SkillButtonViewer;
		
		/**
		 * 显示经验条 
		 */		
		private var expBar:Shape;
		/**
		 * 显示当前等级 
		 */		
		private var gradeSprite:Sprite;
		
		/**
		 * 显示血量和魔法的图形 
		 */		
		private var hmpArc:HMPViewer = null;
		/**
		 * 显示HP的数量标签 
		 */		
		private var hpLabel:TextField = null;
		/**
		 * 显示MP的数量标签 
		 */		
		private var mpLabel:TextField = null;
		//[Embed(source = "../../../resource/skillBG.png")]
		//private var BgPanel:Class;
		private var bgPanel:Bitmap;
		
		[Embed(source = "../../../resource/skill1.png")]
		private var skill1:Class;
		
		[Embed(source = "../../../resource/skill2.png")]
		private var skill2:Class;
		
		[Embed(source = "../../../resource/skill3.png")]
		private var skill3:Class;
		
		[Embed(source = "../../../resource/skill4.png")]
		private var skill4:Class;
		
		[Embed(source = "../../../resource/redDrug.jpg")]
		private var RedDrug:Class;
		/**
		 * 构造函数,初始化技能面板组件 
		 * 
		 */		
		public function SkillPanelViewer()
		{
			super();
			
			initTestData();
			
			bgPanel = new Bitmap(PlatformDepot.getInstance().getBitmapDataByName("SkillBGData"));
			bgPanel.x=0;
			bgPanel.y=0;
			bgPanel.smoothing = true;
			bgPanel.cacheAsBitmap = true;
			var tempSprite:Sprite = new Sprite();
			tempSprite.addEventListener(MouseEvent.CLICK,clickHandle);
			tempSprite.addChild(bgPanel);
			this.addChild(tempSprite);
	       
			hmpArc = new HMPViewer();
			hmpArc.x = 113;
			hmpArc.y = 27;
			this.addChild(hmpArc);
			
			nameLabel();	
//			setHP(200);
//			setMP(300);	
			
			skillBtn1 = new SkillButtonViewer();
			skillBtn1.state = new skill1();
			skillBtn1.setLocation(289, 118);
			skillBtn1.setSizeWH(itemWH, itemWH); 
			skillBtn1.skillId = 0;
			
			skillBtn2 = new SkillButtonViewer();
			skillBtn2.state = new skill2();
			skillBtn2.setLocation(333, 118);
			skillBtn2.setSizeWH(itemWH, itemWH); 
			skillBtn2.skillId = 1;
			
			skillBtn3 = new SkillButtonViewer();
			skillBtn3.state = new skill3();
			skillBtn3.setLocation(377, 118);
			skillBtn3.setSizeWH(itemWH, itemWH); 
			skillBtn3.skillId = 2;
			
			skillBtn4 = new SkillButtonViewer();
			skillBtn4.state = new skill4();
			skillBtn4.setLocation(421, 118);
			skillBtn4.setSizeWH(itemWH, itemWH); 
			skillBtn4.skillId = 3;
			
			skillBtn5 = new SkillButtonViewer();
			skillBtn5.state = new RedDrug();
			skillBtn5.setLocation(465,118);
			skillBtn5.setSizeWH(itemWH,itemWH);
			skillBtn5.skillId = 4;
			skillBtn5.count = ItemDataModel(drugItems.getItemAt(0)).currentCapability;
			 
			expBar = new Shape();
			expBar.x = 279;
			expBar.y = 108;
			//drawExpBar(expBar.graphics,225);
			gradeSprite = new Sprite();
			gradeSprite.x = 282; 
			gradeSprite.y = 95;
			setGrade(1);
			
			this.addChild(skillBtn1);
			this.addChild(skillBtn2);
			this.addChild(skillBtn3);
			this.addChild(skillBtn4);
			this.addChild(skillBtn5);
			this.addChild(expBar);
			this.addChild(gradeSprite);
		}
		
		private function initTestData():void{
			var itemData:ItemDataModel = new ItemDataModel();
			itemData.packageId="f232112222";
			itemData.isSystem=false;
			itemData.tip="fdfdfdfddsd";	
			itemData.capability = 20;
			itemData.capabilityMax = 20;
			itemData.name = "康必得";
			itemData.message = "康必得，中西结合，疗效更好。欢迎使用";
			itemData.itemType = 1000;
			drugItems.addItem(itemData);
		}
		
		private function clickHandle(e:MouseEvent):void
		{
			var charactorService:CharactorService = ObjectCollection.beansContext.getObject("charactorService") as CharactorService;
			
			charactorService.requestPath(e);
		}
		private function drawExpBar(graph:Graphics,barWidth:int):void{
			var colors:Array = null;
			var alphs:Array = [1,1];
			var ratios:Array = [55, 255]; 
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(width,10,Math.PI/2,0,0);
			colors = [0x20ce77,0x1cb065];
			graph.clear();
			graph.lineStyle(1,0x0066ff,0.8);
			graph.beginFill(0xffccff,0.4);
			graph.drawRoundRectComplex(0,0,225,6,3,3,3,3);
			graph.lineStyle(1,0x0066ff,0.8);
			graph.beginGradientFill(GradientType.LINEAR,colors,alphs,ratios,matrix);
			graph.drawRoundRectComplex(0,0,barWidth,6,3,3,3,3);
			graph.endFill();
		}
		
		private function initListener():void{
//			this.skillBtn1.addEventListener(MouseEvent.CLICK,skillClickHandle);
//			this.skillBtn2.addEventListener(MouseEvent.CLICK,skillClickHandle);
//			this.skillBtn3.addEventListener(MouseEvent.CLICK,skillClickHandle);
//			this.skillBtn4.addEventListener(MouseEvent.CLICK,skillClickHandle);
		}
		
		/**
		 * 创建显示主角血和蓝的数量标签
		 * 
		 */		
		private function nameLabel():void{
			var filter:GlowFilter = new GlowFilter();
			var filters:Array = new Array();
			hpLabel = createLabel();
			mpLabel = createLabel();
			
			filter.blurX = 8;
			filter.blurY = 8;
			filter.alpha = 0.65;
			filter.quality = BitmapFilterQuality.HIGH;
			filter.strength = 3;
			filter.knockout = false;
			filter.inner = false;
			filter.color = 0X07D1FD;
			filters.push(filter);
			
			mpLabel.filters = filters;
			hpLabel.filters = filters;	
			
			mpLabel.x = 225;
			mpLabel.y = 150;
			hpLabel.x = 118;
			hpLabel.y = 150;
			
			this.addChild(mpLabel);
			this.addChild(hpLabel);		
		}
		/**
		 * 创建一个文本框 
		 * @return 文本实例
		 * 
		 */		
		private function createLabel():TextField{
			var lab:TextField = new TextField();
			var format:TextFormat = new TextFormat();
			format.font = "Century Gothic";
			format.size = 10;
			lab.selectable = false;
			lab.autoSize = TextFieldAutoSize.CENTER;
			lab.type = TextFieldType.DYNAMIC;
			lab.defaultTextFormat = format;
			lab.width = 48;
			lab.height = 12;
			lab.text = 500 + "/" + 500;
			return lab;
		}
		
		/**
		 * 添加技能CD管理 
		 * @param skillArr 技能对象数组
		 * 
		 */		
		private function addSkillCD(skillArr:Array):void
		{
			//实例化技能CD管理类
			/* SkillCDManager.skillInit();
			for(var i:uint = 0;i<skillArr.length;i++)
			{
				SkillCDManager.setSkillInfo(skillArr[i]);
			} */
		}
		
		/**
		 * 技能按键拖动监听 
		 * @param e 相关监听事件
		 * 
		 */		
		private function skillClickHandle(e:MouseEvent):void
		{
			
		}
	
		/**
		 * 技能快捷键监听 
		 * @param e 相关监听事件
		 * 
		 */		
		private function keySkillHandle(e:KeyboardEvent):void
		{
			if(e.keyCode == 49)//键盘按键"1"
			{
				//useSkill(SkillList.U_SKILL_COMMON,skillBtn1);
			}
			else if(e.keyCode == 50)//键盘按键"2"
			{
				//useSkill(SkillList.U_SKILL_FIREBALL,skillBtn2);
			}
		}

		/**
		 * 设置主UI上主角的生命值 
		 * @param hp 当前生命值
		 * 
		 */		
		public function setHP(hp:int):void{
			var maxHP:int = 500;
			if(hp > maxHP){
				hp = maxHP
			}
			this.hmpArc.setHP(hp);
			this.hpLabel.text = hp + "/" + maxHP;
		}
		/**
		 * 设置主UI上主角的魔法值 
		 * @param mp 当前魔法值
		 * 
		 */		
		public function setMP(mp:int):void{
			var maxMP:int = 500;
			if(mp > maxMP)
				mp = maxMP;
			this.hmpArc.setMP(mp);
			this.mpLabel.text = mp + "/" + maxMP;
		}
		
		public function setEXP(exp:int,max:int):void{
			var wid:int = exp / max * 225;
			drawExpBar(expBar.graphics,wid);
		}
		
		public function setGrade(grade:int):void{
			var tempstr:String = Math.abs(grade).toString();
			var leng:uint = tempstr.length;
			while(gradeSprite.numChildren > 0)
			{
				gradeSprite.removeChildAt(0);
			}
			for(var i:uint = 0;i < leng;i++){
				var figdata:BitmapData = PlatformDepot.getInstance().getBitmapDataByName("Fig"+tempstr.charAt(i));
				var figmap:Bitmap = new Bitmap(figdata);
				figmap.x = gradeSprite.width - 10;
				gradeSprite.addChild(figmap);
			}
		}
	
		
		public function dispose():void
		{
		}
		
	}
}